package com.musicdaze.graphics;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.musicdaze.utils.Utils;

/**
 * Custom frame
 * @author mfujihara
 *
 */
@SuppressWarnings("serial")
public class CustomFrame extends JFrame{
	/** params */
	private JPanel main;
	private JLabel closeLabel;
	private ImageIcon close;
	private ImageIcon closehover;
	private ImageIcon closepress;
	/**
	 * Creates frame with title
	 * @param title the title of the frame
	 */
	public CustomFrame(String title){
		super(title);
		this.setUndecorated(true);
		JPanel total = new JPanel();
		this.setContentPane(total);
		total.setLayout(new BorderLayout());
//		total.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

		
		JPanel closepanel = new JPanel(new BorderLayout());
		try {
			close = new ImageIcon(
					ImageIO.read(Utils.loadResource("close.png")));
			closehover = new ImageIcon(
					ImageIO.read(Utils.loadResource("close_hover.png")));
			closepress = new ImageIcon(
					ImageIO.read(Utils.loadResource("close_press.png")));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		closeLabel = new JLabel(close);
		closeLabel.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
				closeLabel.setIcon(close);
				CustomFrame.this.setVisible(false);
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				closeLabel.setIcon(closepress);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				closeLabel.setIcon(close);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				closeLabel.setIcon(closehover);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		closepanel.add(closeLabel,BorderLayout.EAST);
		closepanel.setBackground(new Color(100,100,100,100));
		new FrameMover(this, closepanel);
		main= new JPanel();
		main.setLayout(new BorderLayout());
		main.setBackground(Color.black);
		
		total.add(closepanel,BorderLayout.NORTH);
		total.add(main,BorderLayout.CENTER);
	}
	public JPanel getMain(){
		return main;
	}
	public static class FrameMover implements MouseMotionListener, MouseListener{
		private Frame frame;
		private boolean pressed;
		private Point last;
		private JPanel panel;

		public FrameMover(Frame frame,JPanel listen){
			this.frame = frame;
			this.panel = listen;
			panel.addMouseMotionListener(this);
			panel.addMouseListener(this);
		}
		@Override
		public void mouseDragged(MouseEvent e) {
			Point now = e.getPoint();
			Point diff = new Point(now.x-last.x, now.y-last.y);
			Point pos = frame.getLocation();
			frame.setLocation(new Point(pos.x+diff.x,pos.y+diff.y));
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			pressed = true;
			last = e.getPoint();
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			pressed = false;
		}
		
	}
}
